package coroutines.coroutine.runblocking

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import java.lang.Exception

class runBlockingTest {
}

fun main() {
    println("Main thread start 0")

    /**
     * runBlocking() 默认在被调用的线程中
     * */
    runBlocking {
        println("runBlocking() thread: ${Thread.currentThread().name}, start ")
        delay(1000)
        println("runBlocking() thread: ${Thread.currentThread().name}, end ")
    }

    println("Main thread start 1")

    try {
        Thread.sleep(100)
    } catch (e: Exception) {
        e.printStackTrace()
    }

    println("Main thread start 2")

    /**
     * runBlocking() 设置在指定的非被调用线程中运行，依旧会阻塞被调用的所在线程
     * */
    runBlocking(context = Dispatchers.IO) {
        println("runBlocking() thread: ${Thread.currentThread().name}, start ")
        delay(1000)
        println("runBlocking() thread: ${Thread.currentThread().name}, end ")
    }

    try {
        Thread.sleep(100)
    } catch (e: Exception) {
        e.printStackTrace()
    }

    println("Main thread start 3")
}